<template>
  <!-- 浅拷贝，修改数据显示到标题 -->
  <div class="article-case">
    <!-- 标题 -->
    <div class="form-preview">
      <h2 v-html="defauleObj.title">如何成为一名前端老鸟？</h2>
      <small>前端</small>
      <a class="btn-edit" href="#" @click="edit">编辑</a>
    </div>

    <!-- 表单 -->
    <div class="form-edit" v-if="isEdit">
      <div class="form-item">
        <div class="label">标题：</div>
        <div class="input">
          <input type="text" placeholder="请输入标题" v-model="form.title" />
        </div>
      </div>
      <div class="form-item">
        <div class="label">频道：</div>
        <div class="input">
          <select v-model="form.subject">
            <option value="">请选择频道</option>
            <option value="前端">前端</option>
            <option value="运维">运维</option>
            <option value="测试">测试</option>
          </select>
        </div>
      </div>
      <div class="form-item">
        <div class="label"></div>
        <div class="input">
          <button class="cancel" @click="confirm">取消</button>
          <button class="submit" @click="confirm">确认</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ArticleCase",
  data() {
    return {
      isEdit: false,

      //用于展示到标题
      defauleObj: {
        subject: "",
        title: "",
      },

      //用于和表单双向数据绑定
      form: {
        subject: "",
        title: "",
      },
    };
  },
  methods: {
    //点击编辑要执行的方法
    edit() {
      this.isEdit = true;
      //浅拷贝 点击编辑后，将标题中的数据和输入框进行双向数据绑定
      //（这里必须用浅拷贝，否则赋值的是地址，修改form中的值的时候会将原本defauleObj中的值也修改掉）
      this.form = { ...this.defauleObj };
    },
    confirm() {
      this.isEdit = false;
      //浅拷贝 点击确定以后将数据显示到标题
      //（这里必须用浅拷贝，否则赋值的是地址，修改defauleObj的值的时候会将原本form中的值也修改掉）
      this.defauleObj = { ...this.form };
    },
  },
};
</script>

<style>
.form-preview {
  position: relative;
  display: flex;
  align-items: center;
}
.form-preview h2 {
  font-size: 18px;
  font-weight: normal;
}
.form-preview small {
  font-size: 12px;
  display: inline-block;
  padding: 2px 6px;
  background: #27ba9b;
  color: #fff;
  border-radius: 2px;
  transform: scale(0.8);
}
.form-preview .btn-edit {
  font-size: 12px;
  display: none;
  text-decoration: none;
  color: #069;
  margin-left: 20px;
}
.form-preview:hover .btn-edit {
  display: block;
}
.form-edit {
  padding-top: 20px;
}
.form-item {
  display: flex;
  margin-bottom: 20px;
  align-items: center;
}
.form-item .label {
  width: 60px;
  text-align: right;
  font-size: 14px;
}
.form-item .input {
  flex: 1;
}
.form-item input,
.form-item select {
  appearance: none;
  outline: none;
  border: 1px solid #ccc;
  width: 200px;
  height: 40px;
  box-sizing: border-box;
  padding: 10px;
  color: #666;
}
.form-item input::placeholder {
  color: #666;
}
.form-item .cancel,
.form-item .submit {
  appearance: none;
  outline: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 10px;
  margin-right: 10px;
  font-size: 12px;
  background: #ccc;
}
.form-item .submit {
  border-color: #069;
  background: #069;
  color: #fff;
}
</style>
